import 'package:flutter/material.dart';
import 'package:flutter_get_x/RouteConfig.dart';
import 'package:flutter_get_x/page/main/logic.dart';
import 'package:flutter_get_x/page/main/state.dart';
import 'package:get/get.dart';
import 'colors.dart' as Colors;

void main() {
  runApp(GetMaterialApp(
    initialRoute: RouteConfig.entrance,
    getPages: RouteConfig.getPages()
  ));
}

class MainPage extends StatelessWidget {
  final MainLogic logic = Get.put(MainLogic());
  final MainSate state = Get.find<MainLogic>().state;
  final PageController _pageController = Get.find<MainLogic>().pageController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Obx(()=> PageView(
          controller: _pageController,
          onPageChanged: (index)=> {
            logic.state.currentIndex.value = index
          },
          children: state.pageList.map((element) => element).toList(),
        )),
      ),
      bottomNavigationBar: Obx(
        () => BottomNavigationBar(
          currentIndex: state.currentIndex.value,
          onTap: (index){
            logic.state.currentIndex.value = index;
            _pageController.animateToPage(index, duration: Duration(milliseconds: 20), curve: Curves.fastOutSlowIn);
          },
          backgroundColor: Color(Colors.APP),
          items: state.bottomItems
              .map((element) =>
                  BottomNavigationBarItem(activeIcon: Icon(element.selectedIcon), backgroundColor: Color(Colors.APP),icon: Icon(element.icon), label: element.text))
              .toList(),
        ),
      ),
    );
  }
}
